package xyz.fivefaith.tool.mybatis.injector;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;

/**
 * @author zhaoanchi
 * @version 1.0
 * @since 2023-09-18 16:08
 */
public abstract class AbstractFaithMethod extends AbstractMethod {

    /**
     * @param methodName 方法名
     * @since 3.5.0
     */
    protected AbstractFaithMethod(String methodName) {
        super(methodName);
    }

    /**
     * EntityWrapper方式获取select where
     *
     * @param newLine 是否提到下一行
     * @param table 表信息
     * @return String
     */
    protected String sqlWhereEntityWrapper(boolean logic, boolean newLine, TableInfo table) {
        String sqlScript;
        if (logic) {
            sqlScript = table.getAllSqlWhere(false, true, true, WRAPPER_ENTITY_DOT);
            sqlScript =
                    SqlScriptUtils.convertIf(
                            sqlScript, String.format("%s != null", WRAPPER_ENTITY), true);
            sqlScript += (NEWLINE + table.getLogicDeleteSql(true, true) + NEWLINE);
            String normalSqlScript =
                    SqlScriptUtils.convertIf(
                            String.format("AND ${%s}", WRAPPER_SQLSEGMENT),
                            String.format(
                                    "%s != null and %s != '' and %s",
                                    WRAPPER_SQLSEGMENT,
                                    WRAPPER_SQLSEGMENT,
                                    WRAPPER_NONEMPTYOFNORMAL),
                            true);
            normalSqlScript += NEWLINE;
            normalSqlScript +=
                    SqlScriptUtils.convertIf(
                            String.format(" ${%s}", WRAPPER_SQLSEGMENT),
                            String.format(
                                    "%s != null and %s != '' and %s",
                                    WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_EMPTYOFNORMAL),
                            true);
            sqlScript += normalSqlScript;
            sqlScript =
                    SqlScriptUtils.convertChoose(
                            String.format("%s != null", WRAPPER),
                            sqlScript,
                            table.getLogicDeleteSql(false, true));
            sqlScript = SqlScriptUtils.convertWhere(sqlScript);
        } else {
            sqlScript = table.getAllSqlWhere(false, true, true, WRAPPER_ENTITY_DOT);
            sqlScript =
                    SqlScriptUtils.convertIf(
                            sqlScript, String.format("%s != null", WRAPPER_ENTITY), true);
            sqlScript += NEWLINE;
            sqlScript +=
                    SqlScriptUtils.convertIf(
                            String.format(
                                    SqlScriptUtils.convertIf(
                                                    " AND",
                                                    String.format(
                                                            "%s and %s",
                                                            WRAPPER_NONEMPTYOFENTITY,
                                                            WRAPPER_NONEMPTYOFNORMAL),
                                                    false)
                                            + " ${%s}",
                                    WRAPPER_SQLSEGMENT),
                            String.format(
                                    "%s != null and %s != '' and %s",
                                    WRAPPER_SQLSEGMENT,
                                    WRAPPER_SQLSEGMENT,
                                    WRAPPER_NONEMPTYOFWHERE),
                            true);
            sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE;
            sqlScript +=
                    SqlScriptUtils.convertIf(
                            String.format(" ${%s}", WRAPPER_SQLSEGMENT),
                            String.format(
                                    "%s != null and %s != '' and %s",
                                    WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_EMPTYOFWHERE),
                            true);
            sqlScript =
                    SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER), true);
        }
        return newLine ? NEWLINE + sqlScript : sqlScript;
    }
}
